package work.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.entity.Card;
import work.entity.User;
import work.service.CardService;
import work.service.UserService;
import work.vo.DataVo;

@RestController
@RequestMapping("/card")
public class CardHandler {

    @Autowired
    private CardService cardService;
    @Autowired
    private UserService userService;

    // 读者挂失借阅证
    @PostMapping("/changeStatus")
    public DataVo changeStatus(@RequestBody User user) {
        DataVo dataVo = new DataVo();

        if (userService.verify(user) == null) {
            dataVo.setCode(220);
            dataVo.setMsg("密码错误");
            return dataVo;
        }

        Card card = new Card();
        card.setUserId(user.getUserId());
        card.setCardStatus("挂失");
        int i = cardService.updateStatus(card);
        if (i == 0) {
            dataVo.setCode(221);
            dataVo.setMsg("挂失失败");
        } else {
            dataVo.setCode(222);
            dataVo.setMsg("挂失成功");
        }
        return dataVo;
    }

    // 工作人员注销读者借阅证
    // 前端传入一个cardId字符串即可
    @PostMapping("/cancelCard")
    public DataVo cancelCard(@RequestBody String cardId) {
        DataVo dataVo = new DataVo();

        Card card = new Card();
        card.setCardId(cardId);
        card.setCardStatus("注销");

        int i = cardService.updateStatusByCardId(card);
        if (i == 0) {
            dataVo.setCode(360);
            dataVo.setMsg("注销失败");
        } else {
            dataVo.setCode(361);
            dataVo.setMsg("注销成功");
        }

        return dataVo;
    }

    // 工作人员冻结读者借阅证
    // 前端传入一个cardId字符串即可
    @PostMapping("/frozenCard")
    public DataVo frozenCard(@RequestBody String cardId) {
        DataVo dataVo = new DataVo();

        Card card = new Card();
        card.setCardId(cardId);
        card.setCardStatus("冻结");

        int i = cardService.updateStatusByCardId(card);
        if (i == 0) {
            dataVo.setCode(370);
            dataVo.setMsg("冻结失败");
        } else {
            dataVo.setCode(371);
            dataVo.setMsg("冻结成功");
        }

        return dataVo;
    }
}
